home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / ge_cool.lha / GE_COOL2.1 / src / Stack / Base_Stack.C next >
C/C++ Source or Header  |  1992-04-22  |  8KB  |  218 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MBN 08/29/89 -- Initial design and implementation
  13. // Updated: MJF 03/12/90 -- Added group names to RAISE
  14. //
  15. // The CoolStack class is  publicly  derived from the  Generic class and is used to
  16. // implement non-type specific functionality for the parameterized CoolStack class.
  17. // In  this manner, code common  to  all instances  of the  CoolStack  class can be
  18. // shared to reduce code replication.  The  CoolStack class  implements a one
  19. // dimensional vector of a user-specified type.  This  is accomplished by using
  20. // the parameterized type  capability of C++.  The  stack will grow dynamically
  21. // as necessary  with   the amount  of  growth determined  by  the value  of an
  22. // allocation size slot.  Fixed length stacks are also supported by setting the
  23. // value of the allocation size slot to zero.
  24. //
  25.  
  26. #ifndef BASE_STACKH                // If no definition for CoolStack
  27. #include <cool/Base_Stack.h>            // Include definition file
  28. #endif
  29.  
  30. // long set_length(long, char*) -- Change number of elements in this stack
  31. // Input:                          Integer number of elements and
  32. //                                 character string of type
  33. // Output:                         Number of elements
  34.  
  35. long CoolStack::set_length (long n, const char* Type) {
  36. #if ERROR_CHECKING
  37.   if (n < 0) {                    // If index out of range
  38.     //RAISE (Error, SYM(CoolStack), SYM(Negative_Length),
  39.     printf ("CoolStack<%s>::set_length(): Negative length %d.\n", Type, n);
  40.     abort ();
  41.   }
  42. #endif
  43.   if (n <= size)                // If not greater than size
  44.     this->number_elements = n;            // Set new length
  45.   else
  46.     this->number_elements = size;        // Else set to maximum possible
  47.   return this->number_elements;            // Return value
  48. }
  49.  
  50.  
  51. // void set_growth_ratio(float, char*) -- Set growth percentage of this stack
  52. // Input:                                 Float ratio and character string type
  53. // Output:                                None
  54.  
  55. void CoolStack::set_growth_ratio (float ratio, const char* Type) {
  56. #if ERROR_CHECKING
  57.   if (ratio <= 0.0) {                // If non-positive growth
  58.     //RAISE (Error, SYM(CoolStack), SYM(Negative_Ratio),
  59.     printf ("CoolStack<%s>::set_growth_ratio(): Negative growth ratio %f.\n",
  60.         Type, ratio);
  61.     abort ();
  62.   }
  63. #endif
  64.   this->growth_ratio = ratio;            // Adjust instance ration
  65.  
  66.  
  67. // void set_alloc_size(int, char*) -- Set the default allocation size 
  68. // Input:                             Integer size and character string type
  69. // Output:                            None
  70.  
  71. void CoolStack::set_alloc_size (int size, const char* Type) {
  72. #if ERROR_CHECKING
  73.   if (size < 0) {                    // If index out of range
  74.     //RAISE (Error, SYM(CoolStack), SYM(Negative_Size),
  75.     printf ("CoolStack<%s>::set_alloc_size(): Negative growth size %d.\n",
  76.         Type, size);
  77.     abort ();
  78.   }
  79. #endif
  80.   this->alloc_size_s = size;            // Set growth size
  81. }
  82.  
  83.  
  84. // CoolStack () -- Empty constructor for the CoolStack class
  85. // Input:            None
  86. // Output:           None
  87.  
  88. CoolStack::CoolStack () {
  89.   this->size = 0;                // Initialize size
  90.   this->number_elements = 0;            // Initialize element count
  91.   if (alloc_size_s == 0)            // If no size specified
  92.     alloc_size_s = STACK_MEM_BLK_SZ;        // Set the default size 
  93.   this->growth_ratio = 0.0;            // Intialize growth ratio
  94. }
  95.  
  96.  
  97. // CoolStack (long) -- constructor that specifies number of elements
  98. // Input:          Integer number of elements
  99. // Output:         None
  100.  
  101. CoolStack::CoolStack (long n) {
  102.   this->size = n;                // Element capacity
  103.   this->number_elements = 0;            // No elements
  104.   if (alloc_size_s == 0)            // If no allocation size
  105.     alloc_size_s = STACK_MEM_BLK_SZ;        // Set default size
  106.   this->growth_ratio = 0.0;            // Initialize growth ratio 
  107. }
  108.  
  109.  
  110. // CoolStack (CoolStack&) -- constructor for reference to another stack
  111. // Input:                        CoolStack reference
  112. // Output:                       None
  113.  
  114. CoolStack::CoolStack (const CoolStack& s) {
  115.   if (alloc_size_s == 0)            // If no allocation size
  116.     alloc_size_s = STACK_MEM_BLK_SZ;        // Default
  117.   this->growth_ratio = s.growth_ratio;        // New growth ratio
  118.   this->size = s.size;                // New size
  119.   this->number_elements = s.number_elements;    // New number of elements
  120. }
  121.  
  122.  
  123. // ~CoolStack -- Destructor for CoolStack class that frees up storage
  124. // Input:          None
  125. // Output:         None
  126.  
  127. CoolStack::~CoolStack () {
  128. }
  129.  
  130.  
  131. // CoolStack& operator= (CoolStack&) -- Assigns this stack to another stack
  132. // Input:                       Reference to a stack
  133. // Output:                      Reference to modified this
  134.  
  135. CoolStack& CoolStack::operator= (const CoolStack& s) {
  136.   this->number_elements = s.number_elements;    // New number of elements
  137.   this->growth_ratio = s.growth_ratio;          // New growth ratio
  138.   return *this;                    // Return reference
  139. }
  140.  
  141.  
  142. // assign_error -- Error message for parameterized CoolStack<Type>::operator=()
  143. // Input:          Character string of type
  144. // Output:         None
  145.  
  146. void CoolStack::assign_error (const char* Type) {
  147.   //RAISE (Error, SYM(CoolStack), SYM(Static_Size),
  148.   printf ("CoolStack<%s>::operator=(): Static-size stack.\n", Type);
  149.   abort ();
  150. }
  151.  
  152.  
  153. // top_error -- Error message for parameterized CoolStack<Type>::top()
  154. // Input:       Character string of type
  155. // Output:      None
  156.  
  157. void CoolStack::top_error (const char* Type) {
  158.   //RAISE (Error, SYM(CoolStack), SYM(No_Elements),
  159.   printf ("CoolStack<%s>::top(): No elements in stack.\n", Type);
  160.   abort ();
  161. }
  162.  
  163.  
  164. // pop_error -- Error message for parameterized CoolStack<Type>::pop()
  165. // Input:       Character string of type
  166. // Output:      None
  167.  
  168. void CoolStack::pop_error (const char* Type) {
  169.   //RAISE (Error, SYM(CoolStack), SYM(No_Elements),
  170.   printf ("CoolStack<%s>::pop(): No elements in stack.\n", Type);
  171.   abort ();
  172. }
  173.  
  174.  
  175. // bracket_error -- Error message for parameterized CoolStack<Type>::operator[]()
  176. // Input:           Character string of type and index
  177. // Output:          None
  178.  
  179. void CoolStack::bracket_error (const char* Type, long n) {
  180.   //RAISE (Error, SYM(CoolStack), SYM(Out_Of_Range),
  181.   printf ("CoolStack<%s>::operator[](): Index %d out of range.\n", Type, n);
  182.   abort ();
  183. }
  184.  
  185.  
  186. // push_error -- Error message for parameterized CoolStack<Type>::push()
  187. // Input:        Character string of type
  188. // Output:       None
  189.  
  190. void CoolStack::push_error (const char* Type) {
  191.   //RAISE (Error, SYM(CoolStack), SYM(Static_Size),
  192.   printf ("CoolStack<%s>::push(): Static-size stack.\n", Type);
  193.   abort ();
  194. }
  195.  
  196.  
  197. // popn_error -- Error message for parameterized CoolStack<Type>::popn()
  198. // Input:        Character string of type and index
  199. // Output:       None
  200.  
  201. void CoolStack::popn_error (const char* Type, long n) {
  202.   //RAISE (Error, SYM(CoolStack), SYM(Static_Size),
  203.   printf ("CoolStack<%s>::popn(): Negative stack index %d.\n", Type, n);
  204.   abort ();
  205. }
  206.  
  207.  
  208. // resize_error -- Error message for parameterized CoolStack<Type>::resize()
  209. // Input:          Character string of type and size
  210. // Output:         None
  211.  
  212. void CoolStack::resize_error (const char* Type, long new_size) {
  213.   //RAISE (Error, SYM(CoolStack), SYM(Negative_Size),
  214.   printf ("CoolStack<%s>::resize(): Negative resize %d.\n", Type, new_size);
  215.   abort ();
  216. }
  217.